package com.tduck.cloud.api.web.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tduck.cloud.api.web.ao.FormUserRelationAO;
import com.tduck.cloud.common.util.Result;
import com.tduck.cloud.form.entity.FormUserRelationEntity;
import com.tduck.cloud.form.service.FormUserRelationService;
import com.tduck.cloud.form.vo.FormUserRelationVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 表单用户关系控制器
 *
 * @author Guard Baby
 * @date 2023/09/09
 */
@RestController
@RequestMapping("/fm/user/realtion")
@RequiredArgsConstructor
@Slf4j
public class FormUserRelationController {
    private final FormUserRelationService formUserRelationService;
    private final FormUserRelationAO formUserRelationAO;

    @RequestMapping("/list/{formKey}")
    public Result<Page<FormUserRelationVo>> queryList(@PathVariable String formKey, Page page) {
        if (StrUtil.isBlank(formKey)) {
            return Result.failed("formKey不能为空");
        }
        QueryWrapper<FormUserRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_key", formKey);
        return Result.success(formUserRelationService.page(page, queryWrapper));
    }
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody FormUserRelationVo formUserRelationVo) {
        return Result.success(formUserRelationAO.save(formUserRelationVo));
    }
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(formUserRelationService.removeById(id));
    }
}
